package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Crw_rule_disabled;
import com.power.sifa.correction.model.pageInfo.Crw_rule_disabledPageInfo;
import com.power.sifa.correction.api.ICrw_rule_disabledFeign;
import com.power.sifa.correction.service.Crw_rule_disabledService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   人员-屏蔽规则控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Crw_rule_disabledController",tags={"人员-屏蔽规则"})
public class Crw_rule_disabledController implements ICrw_rule_disabledFeign{

    @Resource
    private Crw_rule_disabledService crw_rule_disabledService;

    
    @Override
    @ApiOperation("查询人员-屏蔽规则分页列表")
    @SaCheckPermission("crw_rule_disabled_list")
    public ResponseDTO list(Crw_rule_disabledPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Crw_rule_disabled> page = this.crw_rule_disabledService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Crw_rule_disabled crw_rule_disabled : page) {
                crw_rule_disabled.geneText();
                list.add(crw_rule_disabled);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看人员-屏蔽规则")
    @SaCheckPermission("crw_rule_disabled_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Crw_rule_disabled crw_rule_disabled  = crw_rule_disabledService.getById(id);
        if (crw_rule_disabled != null) {
            crw_rule_disabled.geneText();
        }
        return ResponseDTO.succData(crw_rule_disabled);
    }

    @Override
    @ApiOperation("删除人员-屏蔽规则")
    @SaCheckPermission("crw_rule_disabled_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        crw_rule_disabledService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改人员-屏蔽规则")
    @SaCheckPermission("crw_rule_disabled_save")
    public ResponseDTO save(Crw_rule_disabled crw_rule_disabled) {
        Assert.notNull(crw_rule_disabled, "crw_rule_disabled不能为空");
        crw_rule_disabledService.saveOrUpdate(crw_rule_disabled);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除人员-屏蔽规则")
    @SaCheckPermission("crw_rule_disabled_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<String> list = Arrays.asList(ids.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
        crw_rule_disabledService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("crw_rule_disabled_list")
    public void output(HttpServletResponse response, Crw_rule_disabledPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "人员-屏蔽规则";
        String[] heads = {"序号","规则id","规则类型","人员id","屏蔽人员id"};
        String[] fields = {"id","ruleid","ruletype","perid","disabledperid"};
        Page<Crw_rule_disabled> page = this.crw_rule_disabledService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "人员-屏蔽规则";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","规则id","规则类型","人员id","屏蔽人员id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","ruleid","ruletype","perid","disabledperid"};
        List<Crw_rule_disabled> list = (List<Crw_rule_disabled>)ExcelUtil.Import(fields, file, Crw_rule_disabled.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.crw_rule_disabledService.saveAll(list);
        }
        return ResponseDTO.succ();
	}
    
}
